package com.feihong.yw189.bluetooth.startreadcard;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;

import com.feihong.yw189.bluetooth.bleutils.BleGlobUtils;
import com.feihong.yw189.bluetooth.bleutils.BleHttpUtils;
import com.feihong.yw189.bluetooth.listener.UpLoadListener;
import com.kaeridcard.client.BtReaderClient;
import com.kaeridcard.client.IdCardItem;


/**
 * KT8000读卡
 * 2017/8/24
 * 张鹏
 */
public class Kt8000ConnectUtils {

    private BluetoothDevice remoteDevice;
    private String bleAddress;
    private BluetoothAdapter ba;
    private Context context;
    private Activity mActivity;
    private String bleName;
    private String name, sex, nation, id_num, address, sign_office, to, from, born;
    private String birth_year, birth_month, birth_day, sign_data, photoHeard;
    private Bitmap photo;
    private String bluetoothName = "";
    private BleHttpUtils bleHttpUtils;
    private Boolean mFlag = false;
    // 注册成功失败监听
    private UpLoadListener mOnUpLoadListener;
    private BtReaderClient mClient;

    public Kt8000ConnectUtils(Context context, BluetoothAdapter ba, BluetoothDevice remoteDevice,
                              String bleName,  String bleAddress, final String bluetoothName,
                              Boolean flag, final UpLoadListener mOnUpLoadListener) {
        this.context = context;
        this.mActivity = (Activity) context;
        this.ba = ba;
        this.remoteDevice = remoteDevice;
        this.bleName = bleName;
        this.bleAddress = bleAddress;
        this.mFlag = flag;
        this.bluetoothName = bluetoothName;
        this.mOnUpLoadListener = mOnUpLoadListener;
    }

    public void connect(final BtReaderClient mClient) {
        this.mClient = mClient;
        bleHttpUtils = new BleHttpUtils(context, bluetoothName, mFlag, new UpLoadListener() {
            @Override
            public void onSucUpLoad(String bluetoohName, String address, String sex, String id_num,
                                    String nation, String sign_office, String name, String sign_data,
                                    String birth_year, String birth_month, String birth_day,
                                    Bitmap photo, String filePhoto, String idPath, String to,
                                    String from, String born, String photoHeard) {
                mOnUpLoadListener.onSucUpLoad(bluetoothName, address, sex, id_num, nation,
                        sign_office, name, sign_data, birth_year, birth_month, birth_day,
                        photo, filePhoto, idPath, to,
                        from, born, photoHeard);
                // 读卡成功需要断开连接
                if (mClient != null) {
                    mClient.disconnectBt();
                }
            }

            @Override
            public void onErrUpLoad(String err) {
                mOnUpLoadListener.onErrUpLoad(err);
                // 读卡失败需要断开连接
                stopConnect();
            }
        });
        boolean connresult = mClient.connectBt(bleAddress);
        if (connresult) {
            // 读卡操作
            readCard();
        } else {
            mOnUpLoadListener.onErrUpLoad("连接失败,connresult=" + connresult);
        }
    }

    private IdCardItem idcard;

    /**
     * 断开连接
     */
    public void stopConnect() {
        try {
            if (mClient != null) {
                mClient.disconnectBt();
                Log.e("项目异常Kt8000", "------Kt8000断开连接------");
            }
        } catch (Exception e) {
            Log.e("项目异常", "------断开连接异常------" + e.toString());
            mOnUpLoadListener.onErrUpLoad("");
            e.printStackTrace();
        }

    }

    /**
     * KT8000读出身份证信息
     */
    private void readCard() {
        try {
            idcard = mClient.readIDCard();
        } catch (Exception ex) {
            ex.printStackTrace();
            mOnUpLoadListener.onErrUpLoad("读取错误:" + ex.toString());
        }
        if (idcard == null) {
            mOnUpLoadListener.onErrUpLoad("读取失败:idcard为空");
        } else {
            if (idcard.result_code == 0) {
                photo = idcard.picBitmap;
                photoHeard = BleGlobUtils.replaceBlank(
                        BleGlobUtils.bitmapToBase64(photo));
                name = idcard.name.toString().trim();
                sex = idcard.getSexStr(idcard.sex_code);
                nation = idcard.getNationStr(idcard.nation_code);
                born = idcard.birth_year + idcard.birth_month
                        + idcard.birth_day;
                birth_year = idcard.birth_year;
                birth_month = idcard.birth_month;
                birth_day = idcard.birth_day;

                address = idcard.address;
                id_num = idcard.id_num;
                sign_office = idcard.sign_office;
                to = idcard.useful_e_date_year +
                        idcard.useful_e_date_month +
                        idcard.useful_e_date_day;

                from = idcard.useful_s_date_year +
                        idcard.useful_s_date_month +
                        idcard.useful_s_date_day;
                if (to.contains("长期")) {
                    sign_data = idcard.useful_s_date_year
                            + "." + idcard.useful_s_date_month + "." +
                            idcard.useful_s_date_day + "--" +
                            "长期";
                } else {
                    sign_data = idcard.useful_s_date_year
                            + "." + idcard.useful_s_date_month + "." +
                            idcard.useful_s_date_day + "--" +
                            idcard.useful_e_date_year + "."
                            + idcard.useful_e_date_month + "." +
                            idcard.useful_e_date_day;
                }
                Log.e("项目异常", name + "------1------" + id_num);
                bleHttpUtils.upLoadSfzPhoto(bleName,bleAddress,address, photoHeard, sex, id_num, nation,
                        sign_office, to, name, from, born, sign_data, birth_year,
                        birth_month, birth_day,  photo);
            } else {
                mOnUpLoadListener.onErrUpLoad("读取数据失败,idcard.result_code=" + idcard.result_code);
            }
        }
    }
}
